
package com.jf.cloud.distribution.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.distribution.dto.DistributionUserWalletBillDTO;
import com.jf.cloud.distribution.model.DistributionUserWalletBill;
import com.jf.cloud.distribution.service.DistributionUserWalletBillService;
import com.jf.cloud.distribution.vo.DistributionUserVO;
import com.jf.cloud.distribution.vo.DistributionUserWalletBillVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 分销员钱包流水记录
 *
 * @author zz
 * @date 2021-08-09 14:14:10
 */
@RestController("platformDistributionUserWalletBillController")
@RequestMapping("/p/distribution_user_wallet_bill")
@Tag(name = "分销员钱包流水记录")
public class DistributionUserWalletBillController {

    @Autowired
    private DistributionUserWalletBillService distributionUserWalletBillService;

    @Autowired
    private MapperFacade mapperFacade;

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/page")
    @Operation(summary = "获取分销员钱包流水记录列表" , description = "分页获取分销员钱包流水记录列表")
    public ServerResponseEntity<PageVO<DistributionUserWalletBill>> page(@Valid PageDTO pageDTO) {
        PageVO<DistributionUserWalletBill> distributionUserWalletBillPage = distributionUserWalletBillService.page(pageDTO);
        return ServerResponseEntity.success(distributionUserWalletBillPage);
    }

    @GetMapping("/wallet_bill_page")
    @Operation(summary = "获取分销员钱包流水记录列表" , description = "分页获取分销员钱包流水记录列表")
    public ServerResponseEntity<PageVO<DistributionUserWalletBillVO>> walletBillPage(@Valid PageDTO pageDTO, String userMobile) {
        PageVO<DistributionUserWalletBillVO> pageVO = distributionUserWalletBillService.walletBillPage(pageDTO, userMobile);
        if (BooleanUtil.isFalse(permission)) {
            for (DistributionUserWalletBillVO record : pageVO.getList()) {
                DistributionUserVO distributionUser = record.getDistributionUserVO();
                if (Objects.nonNull(distributionUser)) {
                    if (StrUtil.isNotBlank(distributionUser.getNickName()) && PrincipalUtil.isMobile(distributionUser.getNickName())) {
                        distributionUser.setNickName(PhoneUtil.hideBetween(distributionUser.getNickName()).toString());
                    }
                    if (StrUtil.isNotBlank(distributionUser.getUserMobile())) {
                        distributionUser.setUserMobile(PhoneUtil.hideBetween(distributionUser.getUserMobile()).toString());
                    }
                }
            }
        }
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping
    @Operation(summary = "获取分销员钱包流水记录" , description = "根据id获取分销员钱包流水记录")
    public ServerResponseEntity<DistributionUserWalletBill> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(distributionUserWalletBillService.getById(id));
    }

    @PutMapping
    @Operation(summary = "更新分销员钱包流水记录" , description = "更新分销员钱包流水记录")
    public ServerResponseEntity<Void> update(@Valid @RequestBody DistributionUserWalletBillDTO distributionUserWalletBillDTO) {
        DistributionUserWalletBill distributionUserWalletBill = mapperFacade.map(distributionUserWalletBillDTO, DistributionUserWalletBill.class);
        distributionUserWalletBillService.update(distributionUserWalletBill);
        return ServerResponseEntity.success();
    }
}
